<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案例标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入案例标题"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="副标题或简短描述" prop="subtitle">
        <el-input
          v-model="queryParams.subtitle"
          placeholder="请输入副标题或简短描述"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="clientType">
        <el-select v-model="queryParams.clientType" placeholder="请选择客户类型" clearable>
          <el-option v-for="dict in dict.type.achievement_cases_client_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option v-for="dict in dict.type.achievement_cases_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="服务类别" prop="serviceCategory">
        <el-select v-model="queryParams.serviceCategory" placeholder="请选择服务类别" clearable>
          <el-option v-for="dict in dict.type.achievement_cases_service_category" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>


<!--      <el-form-item label="系统用户id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入系统用户id"-->
<!--          filterable clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门id" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门id"-->
<!--          filterable clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- 更多查询条件-->
      <template v-if="isShowMore">
        <el-form-item label="标签" prop="tags">
          <el-input
            v-model="queryParams.tags"
            placeholder="请输入标签"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目周期" prop="projectDuration">
          <el-input
            v-model="queryParams.projectDuration"
            placeholder="请输入项目周期"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="阅读量统计" prop="viewCount">
          <el-input
            v-model="queryParams.viewCount"
            placeholder="请输入阅读量统计"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="排序权重" prop="sortOrder">
          <el-input
            v-model="queryParams.sortOrder"
            placeholder="请输入排序权重"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否已发布" prop="isPublished">
          <el-input
            v-model="queryParams.isPublished"
            placeholder="请输入是否已发布"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否精选" prop="isFeatured">
          <el-input
            v-model="queryParams.isFeatured"
            placeholder="请输入是否精选"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker filterable clearable
                          v-model="queryParams.createdAt"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updatedAt">
          <el-date-picker filterable clearable
                          v-model="queryParams.updatedAt"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>


      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="isShowMore?'el-icon-d-arrow-right':'el-icon-d-arrow-left'" size="mini" type="warning" @click="isShowMore = !isShowMore">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['achievement:cases:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['achievement:cases:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['achievement:cases:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['achievement:cases:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sortedCasesList" @selection-change="handleSelectionChange" stripe :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="序号" align="center" fixed/>
<!--      <el-table-column label="主键ID，自增" align="center" prop="id" />-->
      <el-table-column label="案例标题" align="center" prop="title" width="150"/>
      <el-table-column label="副标题或简短描述" align="center" prop="subtitle" width="150" show-overflow-tooltip/>
      <el-table-column label="封面图URL" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="客户类型" align="center" prop="clientType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.achievement_cases_client_type" :value="scope.row.clientType" />
        </template>
      </el-table-column>
<!--      <el-table-column label="客户LOGO图片URL" align="center" prop="clientLogo" />-->
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.achievement_cases_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="服务类别" align="center" prop="serviceCategory" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.achievement_cases_service_category" :value="scope.row.serviceCategory" />
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tags" />
<!--      <el-table-column label="项目周期" align="center" prop="projectDuration" />-->
<!--      <el-table-column label="标签数组" align="center" prop="tags" />-->
<!--      <el-table-column label="类型：about=关于我们，project=项目" align="center" prop="type" />-->
<!--      <el-table-column label="案例详细内容" align="center" prop="content" />-->
      <el-table-column label="阅读量统计" align="center" prop="viewCount" />
      <el-table-column label="排序权重" align="center" prop="sortOrder" />
      <el-table-column label="是否已发布" align="center" key="isPublished" >
        <template slot-scope="scope">
          <span v-if="scope.row.isPublished === 2">待审核</span>
          <el-switch
            v-else
            v-model="scope.row.isPublished"
            :active-value="1"
            :inactive-value="0"
            @change="handlePublishChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column v-if="!isCommonUser" label="是否精选" align="center" prop="isFeatured" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isFeatured"
            :active-value="1"
            :inactive-value="0"
            @change="handleFeaturedChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="系统用户id" align="center" prop="userId" />-->
<!--      <el-table-column label="部门id" align="center" prop="deptId" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['achievement:cases:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['achievement:cases:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['achievement:cases:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成果展示案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="案例标题" prop="title">
              <el-input v-model="form.title"  placeholder="请输入案例标题" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="副标题或简短描述" prop="subtitle">
              <el-input v-model="form.subtitle"  placeholder="请输入副标题或简短描述" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="封面图URL" prop="coverImage">
              <image-upload v-model="form.coverImage" :fileSize="15"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName"  placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="客户LOGO图片URL" prop="clientLogo">
              <el-input v-model="form.clientLogo" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户类型" prop="clientType">
              <el-select v-model="form.clientType"  placeholder="请输入客户类型" >
                <el-option
                  v-for="dict in dict.type.achievement_cases_client_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type"  placeholder="请输入类型" >
                <el-option
                  v-for="dict in dict.type.achievement_cases_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="服务类别" prop="serviceCategory">
              <el-select v-model="form.serviceCategory"  placeholder="请输入服务类别" >
                <el-option
                  v-for="dict in dict.type.achievement_cases_service_category"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标签" prop="tags">
              <el-input v-model="form.tags"  placeholder="请输入标签" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目周期" prop="projectDuration">
              <el-input v-model="form.projectDuration"  placeholder="请输入项目周期" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="案例详细内容">
              <editor v-model="form.content" :min-height="192" :fileSize="15"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="阅读量统计" prop="viewCount">
              <el-input v-model="form.viewCount"  placeholder="请输入阅读量统计" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序权重" prop="sortOrder">
              <el-input v-model="form.sortOrder"  placeholder="请输入排序权重" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="是否已发布" prop="isPublished">-->
<!--              <el-input v-model="form.isPublished"  placeholder="请输入是否已发布" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
          <el-form-item label="是否已发布">
            <!-- 普通用户新增时只读显示 -->
            <span v-if="isCommonUser">待审核</span>
            <!-- 管理员 或 编辑时可选 -->
            <el-select v-else v-model="form.isPublished">
              <el-option label="未发布" :value="0" />
              <el-option label="已发布" :value="1" />
              <el-option label="待审核" :value="2" />
            </el-select>
          </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="是否精选" prop="isFeatured">-->
<!--              <el-input v-model="form.isFeatured"  placeholder="请输入是否精选" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="创建时间" prop="createdAt">
              <el-date-picker filterable clearable
                              v-model="form.createdAt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择创建时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最后更新时间" prop="updatedAt">
              <el-date-picker filterable clearable
                              v-model="form.updatedAt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择最后更新时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="系统用户id" prop="userId">-->
<!--              <el-input v-model="form.userId"  placeholder="请输入系统用户id" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="部门id" prop="deptId">-->
<!--              <el-input v-model="form.deptId"  placeholder="请输入部门id" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 成果展示案例详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="80%" max-width="1200px" append-to-body>
      <div class="blog-article">
        <!-- 封面图 -->
        <div class="article-cover" v-if="form.coverImage">
          <img
            :src="fixImgUrl(form.coverImage)"
            :style="{ width: '100%', maxHeight: '600px', objectFit: 'cover' }"
          />
        </div>

        <!-- 文章标题区 -->
        <header class="article-header">
          <h1 class="article-title">{{ form.title }}</h1>
          <p class="article-subtitle" v-if="form.subtitle">{{ form.subtitle }}</p>

          <!-- 元信息 -->
          <div class="article-meta">
            <span>客户：{{ form.clientName || '未知' }}</span>
            <span>服务类别：{{ form.serviceCategory || '未分类' }}</span>
<!--            <span>项目周期：{{ form.projectDuration || '未填写' }}</span>-->
            <span>发布时间：{{ parseTime(form.updatedAt, '{y}-{m}-{d}') }}</span>
          </div>
        </header>

        <!-- 正文内容 -->
        <div class="article-content" v-html="form.content">
          <!-- 富文本内容将在此渲染 -->
        </div>

        <!-- 附加信息（可折叠或小字） -->
        <footer class="article-footer">
          <div class="stats">
            <small>阅读量：{{ form.viewCount }} | {{ form.isPublished ? '已发布' : '草稿' }} | {{ form.isFeatured ? '首页推荐' : '普通案例' }}</small>
          </div>
          <div class="meta-info" style="font-size: 12px; color: #999; margin-top: 10px;">
            备注: {{ form.remark || '无' }}
          </div>
        </footer>
      </div>

      <!-- 对话框底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCases, getCases, delCases, addCases, updateCases, updateCasesStatus, updateCasesFeatured } from "@/api/achievement/cases";

export default {
  name: "Cases",
  dicts: ['achievement_cases_client_type','achievement_cases_type','achievement_cases_service_category'],
  data() {
    return {
      // 是否显示更多
      isShowMore: false,
      // el-table 高度
      tableHeight: 650, // 用于存储 el-table 的高度
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成果展示案例表格数据
      // casesList: [],
      rawCasesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subtitle: null,
        coverImage: null,
        clientName: null,
        clientType: null,
        clientLogo: null,
        serviceCategory: null,
        projectDuration: null,
        tags: null,
        type: null,
        content: null,
        viewCount: null,
        sortOrder: null,
        isPublished: null,
        isFeatured: null,
        createdAt: null,
        updatedAt: null,
        userId: null,
        deptId: null,
      },
      // 表单参数
      form: {
        content: ''
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "案例标题，如“XX品牌升级设计”不能为空", trigger: "blur" }
        ],
        coverImage: [
          { required: true, message: "封面图URL，用于首页轮播和列表展示不能为空", trigger: "blur" }
        ],
        // clientName: [
        //   { required: true, message: "客户名称，如“智远科技有限公司”不能为空", trigger: "blur" }
        // ],
        // clientType: [
        //   { required: true, message: "客户类型：企业/学校/个人/政府机构不能为空", trigger: "change" }
        // ],
        type: [
          { required: true, message: "案例类型不能为空", trigger: "change" }
        ],
        // serviceCategory: [
        //   { required: true, message: "服务类别，如“品牌设计”、“软件开发”、“UI/UX设计”不能为空", trigger: "blur" }
        // ],
        // content: [
        //   { required: true, message: "案例详细内容，支持HTML格式不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.setTableHeight();
    window.addEventListener('resize', this.setTableHeight); // 监听窗口大小变化
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setTableHeight); // 移除事件监听
  },
  computed: {
    // 计算属性：返回按 sortOrder 降序排列的列表
    sortedCasesList() {
      return this.rawCasesList
        .slice() // 复制数组，避免修改原数组
        .sort((a, b) => {
          return b.sortOrder - a.sortOrder; // 降序：大值在前
        });
    },
    // 判断角色是否为普通用户
    isAdmin() {
      return this.$store.getters.roles.includes('admin');
    },
    isCommonUser() {
      return this.$store.getters.roles.includes('common');
    }
  },
  methods: {
    /** 查询成果展示案例列表 */
    getList() {
      this.loading = true;
      listCases(this.queryParams).then(response => {
        this.rawCasesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        subtitle: null,
        coverImage: null,
        clientName: null,
        clientType: null,
        clientLogo: null,
        serviceCategory: null,
        projectDuration: null,
        tags: null,
        type: null,
        content: null,
        viewCount: null,
        sortOrder: null,
        isPublished: null,
        isFeatured: null,
        createdAt: null,
        updatedAt: null,
        userId: null,
        deptId: null,
        createBy: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成果展示案例";
      console.log('用户角色:', this.$store.state.user.roles);
      // 新增案例时
      if (this.isAdmin) {
        this.form.isPublished = 0; // 管理员默认“未发布”
      } else {
        this.form.isPublished = 2; // 普通用户“待审核”
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCases(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成果展示案例";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids
      getCases(id).then(response => {
        this.form = response.data;
        this.openInfo = true;
        this.title = "成果展示案例详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCases(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCases(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除成果展示案例编号为"' + ids + '"的数据项？').then(function () {
        return delCases(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('achievement/cases/export', {
        ...this.queryParams
      }, `成果展示案例数据导出_${new Date().getTime()}.xlsx`)
    },
    // 设置表格高度
    setTableHeight() {
      const appContainer = document.getElementById('app-container');
      if (appContainer) {
        this.tableHeight = appContainer.clientHeight * 0.8;
      }
    },
    fixImgUrl(path) {
      if (!path) return '';
      return path.startsWith(process.env.VUE_APP_BASE_API) ? encodeURI(path) : encodeURI(process.env.VUE_APP_BASE_API + path);
    },
    // 处理“是否已发布”开关变化
    handlePublishChange(row) {
      if (!row || !row.id) {
        this.$message.error('ID 不存在');
        return;
      }

      const publishText = row.isPublished === 1 ? '发布' : '取消发布';
      this.$confirm(`确认要${publishText}该案例吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用 API 更新状态
        return updateCasesStatus(row.id, row.isPublished);
      }).then(() => {
        this.$message.success(`${publishText}成功`);
      }).catch(() => {
        // 如果用户取消或请求失败，手动还原开关状态
        row.isPublished = row.isPublished === 1 ? 0 : 1;
      });
    },
    // 处理“是否精选”开关变化
    handleFeaturedChange(row) {
      if (!row || !row.id) {
        this.$message.error('ID 不存在');
        return;
      }
      const featuredText = row.isFeatured === 1 ? '精选' : '取消精选';
      this.$confirm(`确认要${featuredText}该案例吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用 API 更新状态
        return updateCasesFeatured(row.id, row.isFeatured);
      }).then(() => {
        this.$message.success(`${featuredText}成功`);
      }).catch(() => {
        // 如果用户取消或请求失败，手动还原开关状态
        row.isFeatured = row.isFeatured === 1 ? 0 : 1;
      })
    }
  },
};
</script>

<style scoped>
.blog-article {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.8;
  color: #333;
  max-height: 80vh;
  overflow-y: auto;
  padding: 20px;
}

/* 封面图 */
.article-cover {
  text-align: center;
  margin: 0 auto 30px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

/* 标题区 */
.article-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 0 20px;
}

.article-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 10px;
  line-height: 1.2;
}

.article-subtitle {
  font-size: 1.2rem;
  color: #666;
  margin: 0 0 20px;
  font-weight: 400;
}

/* 元信息 */
.article-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
  font-size: 0.9rem;
  color: #777;
  margin-top: 15px;
}

.article-meta span {
  background: #f5f5f5;
  padding: 4px 10px;
  border-radius: 4px;
}

/* 正文内容 */
.article-content {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #444;
  padding: 0 10px;
  /* 关键：确保容器不被压缩 */
  width: 100%;
  word-wrap: break-word; /* 长单词换行 */
}

.article-content h2 {
  font-size: 1.8rem;
  margin: 40px 0 20px;
  color: #222;
  border-left: 4px solid #000;
  padding-left: 15px;
}

.article-content h3 {
  font-size: 1.5rem;
  margin: 30px 0 15px;
  color: #333;
}

.article-content p {
  margin: 20px 0;
  text-align: justify;
}

::v-deep .article-content img {
  max-width: 100% !important;
  height: auto !important;
  display: block;
  margin: 15px 0;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.article-content img:hover {
  transform: scale(1.01);
}

.article-content ul, .article-content ol {
  margin: 20px 0;
  padding-left: 30px;
}

.article-content blockquote {
  border-left: 5px solid #ddd;
  margin: 30px 0;
  padding: 15px 20px;
  background: #f9f9f9;
  font-style: italic;
  color: #555;
}

/* 页脚信息 */
.article-footer {
  margin-top: 50px;
  padding-top: 20px;
  border-top: 1px dashed #eee;
  color: #aaa;
}
</style>
